package com.sky.controller.user;

import com.sky.dto.OrdersDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Slf4j
@Api(tags = "C端-订单模块")
public class OrderController {

    @Autowired
    private OrdersService ordersService;

    @PostMapping("/submit")
    @ApiOperation("提交订单")
    public Result<OrderSubmitVO> submitOrders(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {

        log.info("ordersSubmitDTO={}", ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = ordersService.save(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    @GetMapping("/historyOrders")
    @ApiOperation("显示历史订单")
    public Result<PageResult> getHistoryOrders(Integer page, Integer pageSize, Integer status) {

        OrdersPageQueryDTO ordersPageQueryDTO = new OrdersPageQueryDTO();
        ordersPageQueryDTO.setPage(page);
        ordersPageQueryDTO.setPageSize(pageSize);
        ordersPageQueryDTO.setStatus(status);
        log.info("ordersPageQueryDTO={}", ordersPageQueryDTO);

        PageResult historyOrders = ordersService.getHistoryOrders(ordersPageQueryDTO);
        return Result.success(historyOrders);

    }

    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<String> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) {

        log.info("ordersPaymentDTO={}", ordersPaymentDTO);
        ordersService.payment(ordersPaymentDTO);
        return Result.success();

    }

    @GetMapping("/orderDetail/{orderId}")
    @ApiOperation("订单详情")
    public Result<OrderVO> ordersDetail(@PathVariable Long orderId){

        log.info("orderId={}", orderId);
        OrderVO orderVO = ordersService.getByOrderDetail(orderId);

        return Result.success(orderVO);

    }

    @PutMapping("/cancel/{orderId}")
    @ApiOperation("订单取消")
    public Result<String> cancelOrder(@PathVariable Long orderId){

        ordersService.cancel(orderId);
        return Result.success();

    }

    @PostMapping("/repetition/{orderId}")
    @ApiOperation("再来一单")
    public Result<String> repetitionOrder(@PathVariable Long orderId){

        log.info("orderId={}",orderId);
        ordersService.repetition(orderId);
        return Result.success();

    }

    /**
     * 用户催单
     *
     * @param id
     * @return
     */
    @GetMapping("/reminder/{id}")
    @ApiOperation("用户催单")
    public Result reminder(@PathVariable("id") Long id) {
        ordersService.reminder(id);
        return Result.success();
    }


}
